package com.NeuglsWorkStudio.UI.FormElement.Component
{
	import com.NeuglsWorkStudio.Colors.ColorUtility;
	import com.NeuglsWorkStudio.Colors.HSL;
	import com.NeuglsWorkStudio.Colors.RGB;
	import com.NeuglsWorkStudio.UI.Buttons.TTipButton;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TConfig;
	import com.NeuglsWorkStudio.UI.FormElement.Config.TTitleBarConfig;
	import com.NeuglsWorkStudio.UI.GradientBar;
	
	import flash.display.DisplayObject;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	public class TTitleBar extends TComponent
	{
		[Embed(source="images/MessageBox/closeBtn.png")]
		private var CloseBtnImg:Class;
		
		[Embed(source="images/MessageBox/aui_close.hover.png")]
		private var CloseBtnHoverImg:Class;
		
		
		private var _FBaseColor:int;
		private var _FBackBar:GradientBar;
		private var _FIcon:DisplayObject=null;
		private var _FCaption:String="Title";
		private var _FCaptionField:TextField;
		private var _FTitleFormat:TextFormat;
		private var _FCloseBtn:TTipButton;
		private var _FCloseBtnTip:String;
		private var _FWidth:Number;
		private var _FHeight:Number;
		
		public function TTitleBar(AConfig:TConfig)
		{
			super(AConfig);
		}

		public function get Height():Number
		{
			return _FHeight;
		}

		public function set Height(value:Number):void
		{
			_FHeight = value;
			Resize(_FWidth,value);
		}

		public function get Width():Number
		{
			return _FWidth;
		}

		public function set Width(value:Number):void
		{
			_FWidth = value;
			Resize(value,_FHeight);
		}

		public function get CloseBtnTip():String
		{
			return _FCloseBtnTip;
		}

		public function set CloseBtnTip(value:String):void
		{
			_FCloseBtnTip = value;
			_FCloseBtn.Tip=value;
		}

		public function get TitleFormat():TextFormat
		{
			return _FTitleFormat;
		}

		public function set TitleFormat(value:TextFormat):void
		{
			_FTitleFormat = value;
			
			_FCaptionField.setTextFormat(value);
		}

		public function get Caption():String
		{
			return _FCaption;
		}

		public function set Caption(value:String):void
		{
			_FCaption = value;
			_FCaptionField.text=value;
		}

		public function get Icon():DisplayObject
		{
			return _FIcon;
		}

		public function set Icon(value:DisplayObject):void
		{
			if(_FIcon!=null){
				if(this.contains(_FIcon)){
					this.removeChild(_FIcon);
				}
				_FIcon = value;
				this.addChild(_FIcon);
				Paint();
			}
		}

		/**The Base Color*/
		public function get BaseColor():int
		{
			return _FBaseColor;
		}

		/**
		 * @private
		 */
		public function set BaseColor(value:int):void
		{
			_FBaseColor = value;
			
			Paint();
		}
		
		override protected function Init():void{
			super.Init();
			_FBaseColor=(_FConfig as TTitleBarConfig).BaseColor;
			_FIcon=(_FConfig as TTitleBarConfig).Icon;
			_FCaption=(_FConfig as TTitleBarConfig).Caption;
			
			_FBackBar=new GradientBar(RGB.AddToRGBColor(_FBaseColor,2),_FConfig.Width,_FConfig.Height,false,5,null,true);
			_FCaptionField=new TextField();
			_FCaptionField.autoSize=TextFieldAutoSize.LEFT;
			_FTitleFormat=new TextFormat("Tahoma",16,ColorUtility.GetColorFromBaseColor(_FBaseColor));
			_FTitleFormat.bold=true;
			_FCaptionField.defaultTextFormat=_FTitleFormat;
			_FCaptionField.text=_FCaption;
			_FCaptionField.mouseEnabled=false;
			
			_FCloseBtnTip=(_FConfig as TTitleBarConfig).CloseBtnTip
			_FCloseBtn=new TTipButton('',false);
			_FCloseBtn.Tip=(_FConfig as TTitleBarConfig).CloseBtnTip;
			_FCloseBtn.OnClick=(_FConfig as TTitleBarConfig).OnClose;
			_FCloseBtn.Image=new CloseBtnImg() as DisplayObject;
			_FCloseBtn.MouseOverImg=new CloseBtnHoverImg() as DisplayObject;
			_FCloseBtn.TipBackgroundColor=_FBaseColor;
			_FCloseBtn.AutoAdjustColor=true;
			
			
			this.addChild(_FBackBar);
			this.addChild(_FCaptionField);
			this.addChild(_FCloseBtn);
			
			if(_FIcon!=null){
				this.addChild(_FIcon);
			}
			
			Paint();
		}
		
		protected function Paint():void{
			_FBackBar.BaseColor=_FBaseColor;
			_FBackBar.Resize();
			
			if(_FIcon!=null){
				_FIcon.x=5;
				_FIcon.y=(_FHeight-_FIcon.height)/2;
				
				_FCaptionField.x=_FIcon.x+_FIcon.width+2;
			}else{
				_FCaptionField.x=10;
			}
			
			_FCaptionField.y=(_FHeight-_FCaptionField.textHeight-5)/2;
			
			_FCloseBtn.x=_FWidth-_FCloseBtn.width-10;
			_FCloseBtn.y=(_FHeight)/2;
		}
		
		public function Resize(w:Number,h:Number):void{
			_FWidth=w;
			_FHeight=h;
			_FBackBar.Width=w;
			Paint();
		}

	}
}